import uumoi.Account
import uumoi.Brand
import uumoi.Channel
import uumoi.CompanyInformation
import uumoi.Country
import uumoi.Language
import uumoi.LocalisedText
import uumoi.Region
import uumoi.Role
import uumoi.User
import uumoi.UserRole
import uumoi.World

class BootStrap {
	def grailsApplication

	def init = { servletContext ->
		//TODO remove most of this stuff to somewhere else

		if (!Role.all) {
			println "Creating bootstrap roles and users"

			def roleAdmin = new Role(authority: 'ROLE_ADMIN').save()
			def roleUser = new Role(authority: 'ROLE_USER').save()

			def user = new User(username: 'user', password: 'password', email: 'user@foo.fii', enabled: true).save(failOnError: true)
			def admin = new User(username: 'admin', password: 'password', email: 'admin@foo.fii', enabled: true).save(failOnError: true)
			def krs5 = new User(username: '5krs', password: 'password', email: '5krs@foo.fii', enabled: true).save(failOnError: true)

			UserRole.create krs5, roleUser
			UserRole.create user, roleUser
			UserRole.create admin, roleUser
			UserRole.create admin, roleAdmin, true
		}

		World.withTransaction { tx ->
			World world = World.findByAppVersion(grailsApplication.metadata.getApplicationVersion())

			if (!world) {
				world = new World(appVersion: grailsApplication.metadata.getApplicationVersion())
				loadCountries(world)
				loadLanguages(world)
			}

			def krs5 = new Account()
			krs5.companyInformation = new CompanyInformation(name: "5. Kerros", description: "Kuvaus").save(failOnError: true)
			krs5.save(failOnError: true)

			def suomi = new Region(name: "Suomi", country: Country.findByIso2Code("FI"), account: krs5)
			suomi.save(failOnError: true)

			def ruotsi = new Region(name: "Hurrit", country: Country.findByIso2Code("SE"), account: krs5)
			ruotsi.save(failOnError: true)

			def hoo = new Brand(name: "Höö", description: "Höö lastenvaatteet", account: krs5)
			hoo.save(failOnError: true)

			def hooVerkkokauppSuomenkielinen = new Channel(name: "Höö verkkokauppa - suomi", language: Language.findByIso2Code("fi"), account: krs5)
			hooVerkkokauppSuomenkielinen.addToRegions(suomi)
			hooVerkkokauppSuomenkielinen.addToBrands(hoo)
			hooVerkkokauppSuomenkielinen.save(failOnError: true)

			def kevat2014Hoo = new uumoi.Collection(
				name: "Höö Kevät 2014",
				brand: hoo,
				region: suomi,
				channel: hooVerkkokauppSuomenkielinen,
				startDate: Date.parse("dd.MM.yyyy", "5.5.2014"),
				endDate: Date.parse("dd.MM.yyyy", "30.12.2014"),
				account: krs5).save(failOnError: true)

			def smoi = new Brand(name: "SMOI", description: "Suomalainen Muotoilu On Ihanaa", account: krs5)
			smoi.addToRegions(suomi)
			smoi.addToRegions(ruotsi)
			smoi.save(failOnError: true)

			hoo.addToRegions(suomi)
			krs5.addToBrands(hoo)
			krs5.addToBrands(smoi)

			krs5.save(failOnError: true)

			User u = User.findByUsername("5krs")

			u.addToAccounts(krs5)
			u.save(failOnError: true)

			def emptyAccount = new Account()
			emptyAccount.companyInformation = new CompanyInformation(name: "Firma X", description: "Kuvaus").save(failOnError: true)
			emptyAccount.save(failOnError: true)

			User.findByUsername("user").addToAccounts(emptyAccount).save(failOnError: true)
		}

	}

	def destroy = {
	}

	def loadCountries(World world) {
		new File(getClass().getResource("/countries.csv").toURI()).each { row ->
			println row

			def cols = row.tokenize(";")

			def iso2Code = cols[1].trim()
			def iso3Code = cols[2].trim()
			def enName = cols[4].trim()
			def fiName = cols[5].trim()

			def country = new Country(defaultName: enName, iso2Code: iso2Code, iso3Code: iso3Code)
			country.addToLocalNames(new LocalisedText(iso2Code: "en", text:enName))

			if (fiName)
				country.addToLocalNames(new LocalisedText(iso2Code: "fi", text:fiName))

			country.save(failOnError: true)

			world.addToCountries(country)
		}

		world.save()
	}

	def loadLanguages(World world) {
		new File(getClass().getResource("/languages.csv").toURI()).each { row ->
			def cols = row.tokenize(";")

			println cols

			def iso2Code = cols[0].trim()
			def enName = cols[1].trim()
			def fiName = cols[2].trim()

			def lang = new Language(defaultName: enName, iso2Code: iso2Code)
			lang.addToLocalNames(new LocalisedText(iso2Code: "en", text:enName))
			lang.addToLocalNames(new LocalisedText(iso2Code: "fi", text:fiName))

			lang.save(failOnError: true)

			world.addToLanguages(lang)
		}

		world.save()

		world.addToUiLanguages(Language.findByIso2Code("fi"))
		world.addToUiLanguages(Language.findByIso2Code("en"))

		world.save()
	}
}
